import { $Http } from '@/config/service/http';
import { HOST } from '@/config';
import errImg from '@/assets/img/errImg.svg';
enum Api {
  authCaptcha = 'system/auth/captcha', //获取验证码
  authLogin = 'system/auth/login', //登录
  authLogout = 'system/auth/logout', //退出登录
  routeUpdate = 'system/dev/route/update', //修改路由
  menuAdd = 'system/menu/add', //菜单添加
  menuUpdate = 'system/menu/update', //菜单修改
  dictTypeRefreshCache = 'system/dict/type/refreshCache', //刷新数据字典
  dictGetAll = 'system/dict/type/getAll', //获取数据字典
  uploadUrl = 'dmsfile/resourceFileInfo/uploadFile/5', //上传文件
  downloadFile = 'dmsfile/resourceFileInfo/viewFile' //下载文件
}

const codes = {
  pdm: 1,
  mes: 2,
  aps: 3,
  wms: 4,
  dms: 5,
  qms: 6,
  eam: 7,
  scada: 8,
  dt: 9
};
const systemName = import.meta.env.VITE_PUBLIC_PATH.split('/')
  .find((res) => res.includes('-'))
  .split('-')[0];
/**
 * 上传文件url
 */
export const uploadUrl = `${HOST}/dmsfile/resourceFileInfo/uploadFile/${codes[systemName]}`;

//获取文件路径
export const fileUrl = (id) => {
  if (id) {
    return `${HOST}/dmsfile/resourcePublicFileInfo/viewFile/${id}`;
  } else {
    return errImg;
  }
};

/* 上传文件
 * @param params
 */
export const uploadFile = (params: any = {}, httpOpt: any = {}) => {
  console.log('params', params);
  const formData = new FormData();
  for (const key in params) {
    formData.append(key, params[key]);
  }
  console.log('进入请求', formData);

  return $Http.api(Api.uploadUrl, formData, 'form-data', '', '', httpOpt);
};

/* 下载文件
 * @param params
 */
export const downloadFile = (id) => {
  return $Http.api(Api.downloadFile, id, 'get-form');
};
/**
 * 登录
 * @param params
 */
export const authLogin = (params: any = {}) => {
  return $Http.api(Api.authLogin, params, 'post');
};

/**
 * 退出登录
 * @param params
 */
export const authLogout = () => {
  return $Http.api(Api.authLogout, {}, 'post');
};

/**
 * 验证码
 * @param params
 */
export const authCaptcha = (params: any = {}) => {
  return $Http.api(Api.authCaptcha, params, 'get', '', '', { responseType: 'blob' });
};

/**
 * 路由修改
 * @param params
 */
export const routeUpdate = (params: any = {}) => {
  return $Http.api(Api.routeUpdate, params, 'post');
};

/**
 * 菜单添加
 * @param params
 */
export const menuAdd = (params: any = {}) => {
  return $Http.api(Api.menuAdd, params, 'post');
};

/**
 * 菜单修改
 * @param params
 */
export const menuUpdate = (params: any = {}) => {
  return $Http.api(Api.menuUpdate, params, 'post');
};

/**
 * 刷新数据字典
 * @param params
 */
export const dictTypeRefreshCache = (params: any = {}) => {
  return $Http.api(Api.dictTypeRefreshCache, params, 'post');
};

/**
 * 获取数据字典
 * @param params
 */
export const dictGetAll = () => {
  return $Http.api(Api.dictGetAll, {}, 'post');
};
